/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc.controller;

import cn.yzc.pojo.Result;
import cn.yzc.pojo.User;
import cn.yzc.service.UserService;
import cn.yzc.utils.TokenUtils;
import cn.yzc.utils.group.Insert;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: SpringBootPro - UserController</p>
 * <p>Powered by yzc On 2023-11-10 21:48:28</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Api(tags = "用户管理")
@Slf4j
@RestController
@RequestMapping("/api_V1.0")
@CrossOrigin
public class UserController extends BaseController{
    @Autowired
   UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/user/register")
    public Result register(@Validated(value = {Insert.class}) User user, BindingResult result){
     validator(result);
    int rs = userService.userRegister(user);
    log.info("用户注册,参数:{},{}",user.getUsername(),user.getPassword());
    if (rs == 1)
        return Result.success("注册成功");
    return Result.error("注册失败");
    }

    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true)
    })
    @PostMapping("/user/login")
    public Result login( String username, String password ){

        int rs = userService.userLogin(username, password);
        if (rs != 1){
            return Result.error("登录失败，请检查自己的账号及密码是否正确");
        }
        String token =TokenUtils.getToken(username, password);
        Map map = new HashMap<>();
        map.put("message","登录成功");
        map.put("token",token);
        return Result.success(map);
    }

    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @DeleteMapping("user/del")
    public Result del(String id){
      return Result.success();
    }
}
